package flc.ast.fragment.timer;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.os.Handler;
import android.os.SystemClock;
import android.view.View;

import java.util.ArrayList;
import java.util.List;

import flc.ast.R;
import flc.ast.adapter.SubsectionAdapter;
import flc.ast.bean.MySubsectionBean;
import flc.ast.databinding.FragmentSecondBinding;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.event.EventStatProxy;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link SecondFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class SecondFragment extends BaseNoModelFragment<FragmentSecondBinding> {

    private boolean isStart;
    private long startTime, pausedTime;
    private List<MySubsectionBean> listSubsection = new ArrayList<>();
    private SubsectionAdapter subsectionAdapter;

    private Handler mhandler = new Handler();
    private Runnable runnable = new Runnable() {
        @Override
        public void run() {
            long currentTime = SystemClock.uptimeMillis() - startTime + pausedTime;
            mDataBinding.tvTime.setText(formatTime(currentTime)); // 更新UI显示时间
            mhandler.postDelayed(this, 10); // 每10毫秒更新一次
        }
    };


    @Override
    protected int onCreate() {
        return R.layout.fragment_second;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent5(getActivity(), mDataBinding.event5Container);

        mDataBinding.ivSubsection.setOnClickListener(this);
        mDataBinding.ivReset.setOnClickListener(this);
        mDataBinding.ivStart.setOnClickListener(this);

        mDataBinding.rvSubsectionList.setLayoutManager(new LinearLayoutManager(mContext));
        subsectionAdapter = new SubsectionAdapter();
        mDataBinding.rvSubsectionList.setAdapter(subsectionAdapter);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivStart://启动/停止
                if (!isStart) {
                    startTime = SystemClock.uptimeMillis();
                    mhandler.post(runnable);
                    isStart = true;
                    mDataBinding.ivStart.setImageResource(R.drawable.atz);
                    mDataBinding.ivSubsection.setImageResource(R.drawable.afd);
                    mDataBinding.ivSubsection.setVisibility(View.VISIBLE);
                    mDataBinding.ivReset.setVisibility(View.GONE);
                } else {
                    pausedTime += SystemClock.uptimeMillis() - startTime;
                    mhandler.removeCallbacks(runnable);
                    isStart = false;
                    mDataBinding.ivStart.setImageResource(R.drawable.aqd);
                    mDataBinding.ivSubsection.setVisibility(View.GONE);
                    mDataBinding.ivReset.setVisibility(View.VISIBLE);
                }
                break;
            case R.id.ivSubsection://分段
                if (isStart) {
                    setSubSectionData();
                }
                break;
            case R.id.ivReset://复位
                resetTimer();
                break;

        }
    }

    @Override
    protected void initData() {

    }

    private void resetTimer() {
        mhandler.removeCallbacks(runnable);
        startTime = 0L;
        pausedTime = 0L;
        listSubsection.clear();
        subsectionAdapter.setList(listSubsection);
        isStart = false;
        mDataBinding.tvTime.setText(getString(R.string.second_zero));
        mDataBinding.ivStart.setImageResource(R.drawable.aqd);
        mDataBinding.ivSubsection.setImageResource(R.drawable.afdw);
        mDataBinding.ivSubsection.setVisibility(View.VISIBLE);
        mDataBinding.ivReset.setVisibility(View.GONE);
    }

    private void setSubSectionData() {
        if (isStart) {
            long currentTime = SystemClock.uptimeMillis() - startTime + pausedTime;
            int minutes = (int) (currentTime % 3600000) / 60000;
            int seconds = (int) (currentTime % 60000) / 1000;
            int ms = (int) (currentTime % 1000) / 10;
            listSubsection.add(0, new MySubsectionBean(getString(R.string.subsection_text) + (listSubsection.size() + 1), String.format("%02d:%02d:%02d", minutes, seconds, ms)));
            subsectionAdapter.setList(listSubsection);
        }
    }

    private String formatTime(long millis) {
        int hours = (int) (millis / 3600000);
        int minutes = (int) (millis % 3600000) / 60000;
        int seconds = (int) (millis % 60000) / 1000;
        int ms = (int) (millis % 1000) / 10;
        return String.format("%02d:%02d.%02d", minutes, seconds, ms);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mhandler.removeCallbacks(runnable); // 防止内存泄漏
    }

}